<?php
/**
 * Hot deal of products
 * Author: mr.Trieu
 */

if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')){
	exit('Access Denied');
}

cpheader();

switch ($operation){
	case 'hot':
		shownav('shop', 'shop_admin_product_hot');
		showsubmenu('shop_admin_product_hot');
		showtips('shop_pro_hot_tips');

		showformheader('products', '', 'prosearch');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		$showHot = showHot();

		showtableheader();
		showsubtitle(array(
				'display_order',
				'',
				'shop_admin_product_hot',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'Loại bỏ'
		));

		$i = 0;
		foreach ($showHot as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td>' . $i . '</td>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td>' . $value['pname'] . '</td>';
			echo '<td>' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Xem chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=removehot&amp;pid=' . $value['pid'] . '">Loại (x)</a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="8"'), array(''));
		showtablefooter();
	break;
	case 'new':
		shownav('shop', 'shop_admin_product_new');
		showsubmenu('shop_admin_product_new');
		showtips('shop_pro_new_tips');

		showformheader('products', '', 'prosearch');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		$showNew = showNew();

		showtableheader();
		showsubtitle(array(
				'display_order',
				'',
				'Sản phẩm mới',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'Loại bỏ'
		));

		$i = 0;
		foreach ($showNew as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td>' . $i . '</td>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td>' . $value['pname'] . '</td>';
			echo '<td>' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Xem chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=removehot&amp;pid=' . $value['pid'] . '">Loại (x)</a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="8"'), array(''));
		showtablefooter();
	break;
	case 'bestsale':
		shownav('shop', 'shop_admin_product_best_sale');
		showsubmenu('shop_admin_product_best_sale');

		showformheader('products', '', 'prosearch');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		$bestSale = bestSale();

		showtableheader();
		showsubtitle(array(
				'display_order',
				'',
				'shop_admin_product_best_sale',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'Loại bỏ'
		));

		$i = 0;
		foreach ($bestSale as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td>' . $i . '</td>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td>' . $value['pname'] . '</td>';
			echo '<td>' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Xem chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=removesale&amp;pid=' . $value['pid'] . '">Loại (x)</a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="8"'), array(''));
		showtablefooter();
	break;
	case 'bestprice':
		shownav('shop', 'shop_admin_product_best_price');
		showsubmenu('shop_admin_product_best_price');

		showformheader('products', '', 'prosearch');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		$bestPrice = bestPrice();

		showtableheader();
		showsubtitle(array(
				'display_order',
				'',
				'shop_admin_product_best_price',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'Loại bỏ'
		));

		$i = 0;
		foreach ($bestPrice as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td>' . $i . '</td>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td>' . $value['pname'] . '</td>';
			echo '<td>' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Xem chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=removeprice&amp;pid=' . $value['pid'] . '">Loại (x)</a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="8"'), array(''));
		showtablefooter();
	break;
	case 'hotdeal':
		shownav('shop', 'shop_admin_product_hot_deal');
		showsubmenu('shop_admin_product_hot_deal');

		showformheader('products', '', 'prosearch');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		$hotDeal = hotDeal();

		showtableheader();
		showsubtitle(array(
				'display_order',
				'',
				'shop_admin_product_hot_deal',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'Loại bỏ'
		));

		$i = 0;
		foreach ($hotDeal as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td>' . $i . '</td>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td>' . $value['pname'] . '</td>';
			echo '<td>' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Xem chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=removedeal&amp;pid=' . $value['pid'] . '">Loại (x)</a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="8"'), array(''));
		showtablefooter();
	break;
	
	case 'addnew':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET new=1 WHERE pid='$pid'");
		}

		updatecache('proNew');
		cpmsg('add_pro_new_succeed', 'action=products&operation=new', 'succeed');
	break;
	case 'removenew':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET new=0 WHERE pid='$pid'");
		}

		updatecache('proNew');
		cpmsg('remove_pro_new_succeed', 'action=products&operation=new', 'succeed');
	break;
	case 'addhot':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET hot=1 WHERE pid='$pid'");
		}

		updatecache('proHot');
		cpmsg('add_pro_hot_succeed', 'action=products&operation=hot', 'succeed');
	break;
	case 'removehot':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET hot=0 WHERE pid='$pid'");
		}

		updatecache('proHot');
		cpmsg('remove_pro_hot_succeed', 'action=products&operation=hot', 'succeed');
	break;
	case 'addsale':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET bestsale=1 WHERE pid='$pid'");
		}

		updatecache('bestSale');
		cpmsg('add_pro_best_sale_succeed', 'action=products&operation=bestsale', 'succeed');
	break;
	case 'removesale':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET bestsale=0 WHERE pid='$pid'");
		}

		updatecache('bestSale');
		cpmsg('remove_pro_best_sale_succeed', 'action=products&operation=bestsale', 'succeed');
	break;
	case 'addprice':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET bestprice=1 WHERE pid='$pid'");
		}

		updatecache('bestPrice');
		cpmsg('add_pro_best_price_succeed', 'action=products&operation=bestprice', 'succeed');
	break;
	case 'removeprice':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET bestprice=0 WHERE pid='$pid'");
		}

		updatecache('bestPrice');
		cpmsg('remove_pro_best_price_succeed', 'action=products&operation=bestprice', 'succeed');
	break;
	case 'adddeal':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET hotdeal=1 WHERE pid='$pid'");
		}

		updatecache('hotDeal');
		cpmsg('add_pro_hot_deal_succeed', 'action=products&operation=hotdeal', 'succeed');
	break;
	case 'removedeal':
		$pid = $_G['gp_pid'];
		$pid = intval($pid);

		if ($pid){
			DB::query("UPDATE ".DB::table('shop_products')." SET hotdeal=0 WHERE pid='$pid'");
		}

		updatecache('hotDeal');
		cpmsg('remove_pro_hot_deal_succeed', 'action=products&operation=hotdeal', 'succeed');
	break;
	
	default:
		$keyword = $_G['gp_keyword'];
		$result = search($keyword);

		shownav('shop', 'search_products');
		showsubmenu('search_result_products');
		showtips('search_products_tips');

		showformheader('products');
		echo '<div style="height:30px;line-height:30px;"><a>Tìm sản phẩm</a> <input type="text" name="keyword" value="' . $keyword . '" class="txt" /> <input type="submit" class="btn" value="' . cplang('search') . '" /></div>';
		showformfooter();

		showtableheader();
		showsubtitle(array(
				'IMG',
				'Sản phẩm',
				'Giá',
				'Giảm giá',
				'Cửa hàng bán',
				'Liên kết',
				'<img src="/static/shop/images/icon_hot.gif" />',
				'<img src="/static/shop/images/icon_new.gif" />',
				'BC',
				'GT',
				'SH'
		));

		$i = 0;
		foreach ($result as $key=>$value){
			$i++;
			echo '<tr>';
			echo '<td><img src="' . $value['imgcoverthumb'] . '" widht="32" height="32" /></td>';
			echo '<td width="40%">' . $value['pname'] . '</td>';
			echo '<td nowrap="nowrap">' . $value['pprice'] . '</td>';
			echo '<td>' . $value['sales'] . '%</td>';
			echo '<td>' . $value['shopname'] . '</td>';
			echo '<td><a href="shop.php?name=' . locdau($value['pname']) . '&amp;pid=' . $value['pid'] . '" target="_blank">Chi tiết</a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=addhot&amp;pid=' . $value['pid'] . '" title="Nổi bật"><img src="/static/image/common/app_add.gif" /></a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=addnew&amp;pid=' . $value['pid'] . '" title="Mới"><img src="/static/image/common/app_add.gif" /></a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=addsale&amp;pid=' . $value['pid'] . '" title="Bán chạy"><img src="/static/image/common/app_add.gif" /></a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=addprice&amp;pid=' . $value['pid'] . '" title="Giá tốt"><img src="/static/image/common/app_add.gif" /></a></td>';
			echo '<td><a href="admin.php?action=products&amp;operation=adddeal&amp;pid=' . $value['pid'] . '" title="Giá shock"><img src="/static/image/common/app_add.gif" /></a></td>';
			echo '</tr>';
		}

		showtablerow('', array('colspan="11"'), array(''));
		showtablefooter();
	break;
}

/*
 * San pham noi bat
 * Hot products
 * */
function showHot(){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['proHot'])){
		loadcache('proHot');
	}
	$products = &$_G['cache']['proHot'];
//	var_dump($products);

	return $products;
}

/*
 * San pham moi
 * New products
 * */
function showNew(){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['proNew'])){
		loadcache('proNew');
	}
	$products = &$_G['cache']['proNew'];
//	var_dump($products);

	return $products;
}

/*
 * San pham ban chay
 * Best Sale products
 * */
function bestSale(){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['bestSale'])){
		loadcache('bestSale');
	}
	$products = &$_G['cache']['bestSale'];
//	var_dump($products);

	return $products;
}

/*
 * Gia tot moi ngay
 * Best price products
 * */
function bestPrice(){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['bestPrice'])){
		loadcache('bestPrice');
	}
	$products = &$_G['cache']['bestPrice'];
//	var_dump($products);

	return $products;
}

/*
 * Gia Shock
 * Shock products
 * */
function hotDeal(){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['hotDeal'])){
		loadcache('hotDeal');
	}
	$products = &$_G['cache']['hotDeal'];
//	var_dump($products);

	return $products;
}

/*
 * Products search
 * @author mr.Trieu
 * */
function search($keyword){
	global $_G;
	$products = array();

	if (!empty($keyword)){
		$mySelect = ", if(p.pname LIKE '{$keyword}', 50, 1) +
					if(p.pname LIKE '%{$keyword}%', 50, 2) +
					if(p.pname LIKE '%{$keyword}', 50, 3) +
					if(p.pname LIKE '{$keyword}%', 50, 4)";

		if (! empty($mySelect)){
			$mySelect .= ' AS nPoint';
			$mySqli = 'HAVING nPoint >= 50';
			$myOrder = 'nPoint DESC';
		}

		$Qry = 'SELECT p.pid, p.pname, p.img_cover, a.attachment, p.pprice, p.sales, p.new, p.hot, m.*' . $mySelect . '
				FROM ' . DB::table("shop_products") . ' p
				LEFT JOIN ' . DB::table("shop_products_attachment") . ' a ON (p.img_cover = a.aid)
				LEFT JOIN ' . DB::table("shop_profile") . ' m ON p.uid = m.uid
				WHERE available = 1
				' . $mySqli . '
				ORDER BY ' . $myOrder . '
				LIMIT 0,30';
//		echo $Qry;
		$res = mysql_query($Qry);

		while ($row = DB::fetch($res)){
			$row['imgcover'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . $row['attachment'];
			$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';

			if ($row['sales']){
				$row['pprice'] = ($row['pprice'] - (doubleval($row['pprice']) * doubleval($row['sales'])) / 100);
				$row['pprice'] = FormatNumber($row['pprice']);
				$row['sales'] = FormatNumber($row['sales'], 2, '.', ",", FALSE);
			}else
				$row['end_price'] = FormatNumber($row['pprice']);

			$products[] = $row;
		}
	}

	return $products;
}


